package com.njtcedu.microblog.intranet.user.web;

import java.util.Map;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.njtcedu.microblog.common.web.SessionConstants;
import com.njtcedu.microblog.intranet.user.service.UserService;
import com.njtcedu.microblog.intranet.user.web.request.UserGenerateRequest;
import com.njtcedu.microblog.intranet.user.web.request.UserSearchRequest;

@Controller
@RequestMapping("/intranet")
public class UserController {
	
	@Inject
	private UserService userService;
	
	private final static int INIT_PAGE_NO = 1;
	private final static int INIT_PAGE_SIZE = 10;
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String toLoginPage(Map<String,Object> map) {
		return "user_login";
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(HttpServletRequest request, Map<String,Object> map) {
		UserSearchRequest searchRequest = (UserSearchRequest) request.getSession().getAttribute(SessionConstants.USER_SEARCH_REQUEST);
		map.put("pageModel", userService.userList(searchRequest, INIT_PAGE_NO, INIT_PAGE_SIZE));
		return "user_list";
	}
	@RequestMapping(value = "/user", method = RequestMethod.GET)
	public String toCreateUserPage() {
		return "user_setup";
	}
	
	@RequestMapping(value = "/user", method = RequestMethod.POST)
	public String createUser(UserGenerateRequest request, Map<String,Object> map) {
		
		try {
			userService.createUser(request);
			UserSearchRequest searchRequest = new UserSearchRequest();
			map.put("pageModel", userService.userList(searchRequest, INIT_PAGE_NO, INIT_PAGE_SIZE));
		} catch (Exception e) {
			map.put("exception", e);
			map.put("user", request);
			return "user_setup";
		}
		return "user_list";
	}
	
	@RequestMapping(value = "/userlist/{pageSize}", method = {RequestMethod.POST, RequestMethod.GET})
	public String userList(HttpServletRequest request, UserSearchRequest searchRequest, @PathVariable() Integer pageSize, Map<String, Object> map) {
		request.getSession().setAttribute(SessionConstants.USER_SEARCH_REQUEST, searchRequest);
		map.put("pageModel", userService.userList(searchRequest, INIT_PAGE_NO, pageSize));
		return "user_list";
	}
	
	@RequestMapping(value = "/userlist/{pageNo}/{pageSize}", method = RequestMethod.GET)
	public String userList(HttpServletRequest request,@PathVariable() Integer pageSize, @PathVariable() Integer pageNo, Map<String, Object> map) {
		UserSearchRequest searchRequest = (UserSearchRequest) request.getSession().getAttribute(SessionConstants.USER_SEARCH_REQUEST);
		map.put("pageModel", userService.userList(searchRequest, pageNo, pageSize));
		return "user_list";
	}
	
	@RequestMapping(value = "/boutique", method = RequestMethod.GET)
	public String boutiqueSetup(){
		return "boutique_setup";
	}
}
